//
//  KRRepairReportDetailVC.m
//  KEREN
//
//  Created by MW on 2023/4/23.
//

#import "KRRepairReportDetailVC.h"
#import "KRSettingCell.h"
#import "KRTextCell.h"
#import "GaragelistViewController.h"
@interface KRRepairReportDetailVC ()<UITableViewDelegate,UITableViewDataSource>
{
    NSArray *_titleArr;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UILabel *photoLbl;
@property (nonatomic, strong) UIView *footerView;


@end

@implementation KRRepairReportDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavViewWithRightTitle:@"" navColorIswhite:YES withTitle:@"在线报修"];
    _titleArr = @[@"车辆选择",@"网点名称",@"维修时间",@"文字说明"];
    [self tableView];
    if(_type == 1001){
        if(_model.status == 0){
            self.tableView.tableFooterView = [self footerView];
        }
    }


}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _titleArr.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [UIView new];
    headView.backgroundColor = [UIColor clearColor];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 3){
        return tableView.estimatedRowHeight;
    }else{
        return 60;

    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return  [self bottomView];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    float imageWidth = (DEVICE_WIDTH - 24 * 4) / 3;
    if(_model.photo.count > 3){
        return imageWidth * 2 + 84;
    }else{
        if(_model.photo.count == 0){
            return 0.1;
        }else{
            return imageWidth + 64;
        }
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 3){
        KRTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KRTextCell"];
        cell.titleLbl.text = _titleArr[indexPath.row];
        cell.decLbl.text = _model.desc;
        return cell;
    }
    KRSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KRSettingCell"];
    cell.nameLbl.text = _titleArr[indexPath.row];
    cell.rightIcon.hidden = YES;
    cell.photoIcon.hidden = YES;
    //RepairModel
    if(_type == 1001){
        cell.decLeftConstraint.constant = 0;
        if(indexPath.row == 0){
            cell.decLbl.text = _model.ebike_type_name;
        }else if (indexPath.row == 1){
            cell.decLbl.text = _model.store_name;
        }else if (indexPath.row == 2){
            cell.decLbl.text = [[DeteManager shareInstance] timeFormatYYYYMMdd:_model.created_at];
        }
        
    }
    if(indexPath.row == _titleArr.count - 1){
        cell.lineView.hidden = YES;
    }else{
        cell.lineView.hidden = NO;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if(_type == 1001){
        return;
    }
}

- (void)chexiao{
    WEAKSELF
    [NetworkingTool POST:@"/v1/app/repair/recall" parameters:@{@"id":@(_model.Id)} isDeviceUrl:YES success:^(id  _Nonnull response, NSURLSessionDataTask * _Nonnull operation) {
        if([response[@"code"] intValue] == 0){
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(NSError * _Nonnull error, NSURLSessionDataTask * _Nonnull operation) {
        
    }];
    ///
}

- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NAVTOP_Height, DEVICE_WIDTH, DEVICE_HEIGHT - NAVTOP_Height) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Main_BackColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 60;
        //_tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
       // _tableView.rowHeight = estimatedRowHeight;
        
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }

        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"KRSettingCell" bundle:nil] forCellReuseIdentifier:@"KRSettingCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"KRTextCell" bundle:nil] forCellReuseIdentifier:@"KRTextCell"];        
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
- (UIView *)bottomView{
    if(!_bottomView){
        float imageWidth = (DEVICE_WIDTH - 24 * 4) / 3;
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = Whire_Color;

        if(_model.photo.count > 3){
            _bottomView.frame = CGRectMake(0, 0, DEVICE_WIDTH, imageWidth * 2 + 84);
        }else{
            _bottomView.frame = CGRectMake(0, 0, DEVICE_WIDTH, imageWidth + 64);
        }
        _photoLbl = [UILabel setLblText:[NSString stringWithFormat:@"上传图片（%ld/6）",_model.photo.count] font:Font(16) withColor:Color_333333 inSubView:_bottomView];
        [_photoLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(16);
            make.left.offset(24);
        }];
        if(_model.photo.count == 0){
            _photoLbl.hidden = YES;
        }else{
            _photoLbl.hidden = NO;
        }
        for (int i = 0; i < _model.photo.count; i ++) {
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.layer.cornerRadius = 16;
            imageView.clipsToBounds = YES;
            [_bottomView addSubview:imageView];
            if(i < 3){
                [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.offset(24 + i * (imageWidth + 24));
                    make.width.height.offset(imageWidth);
                    make.top.equalTo(_photoLbl.mas_bottom).offset(16);
                }];
            }else{
                [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.offset(24 + (i - 3) * (imageWidth + 24));
                    make.width.height.offset(imageWidth);
                    make.top.equalTo(_photoLbl.mas_bottom).offset(32 + imageWidth);
                }];

            }
            NSString *photo = _model.photo[i];
            if(photo){
                if(![photo hasPrefix:@"http"]){
                    photo = [NSString stringWithFormat:@"%@%@",BaseimageUrl,photo];
                }
                photo = [photo stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

            }
            [imageView sd_setImageWithURL:[NSURL URLWithString:photo] placeholderImage:[UIImage imageNamed:@"devcie_defalaut_icon"] options:SDWebImageRefreshCached];
        }
    }
    return _bottomView;
}

- (UIView *)footerView{
    if(!_footerView){
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, 48 + 64 + BottomSpance_height)];
        UIButton *chxBtn = [UIButton setButtonWithTitle:@"撤回报修单" titleColor:Whire_Color titleFont:BFont(16) cornerRadius:25 backColor:Color_7ACC00];
        [_footerView addSubview:chxBtn];
        [chxBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-24);
            make.width.offset(DEVICE_WIDTH - 48);
            make.height.offset(48);
            make.bottom.offset(-BottomSpance_height);
        }];
        [chxBtn addTarget:self action:@selector(chexiao) forControlEvents:UIControlEventTouchUpInside];
    }
    return _footerView;

}



@end
